﻿@using System.Globalization;
@using Smartstore.Core.Content.Media;
@using Smartstore.Utilities;
@using Smartstore.Web.Models.Catalog;

@model ProductSummaryModel
@inject MediaSettings MediaSettings

@{
    var id = "artlist-" + CommonHelper.GenerateRandomDigitCode(10);

    // TODO: (mc) Fetch this from the (new) parent model or the product category
    var thumbAspectRatio = MediaSettings.DefaultThumbnailAspectRatio;
    var imgRatio = string.Empty;

    if (Model.ViewMode != ProductSummaryViewMode.List && thumbAspectRatio != 1 && thumbAspectRatio > 0.2 && thumbAspectRatio < 2)
    {
        imgRatio = thumbAspectRatio.ToString(CultureInfo.InvariantCulture);
    }

    var hasPager = (Model.AvailablePageSizes != null && Model.AvailablePageSizes.Count() > 1) || Model.PagedList.TotalPages > 1;

    var hasActions = Model.PagedList.TotalPages > 1
        || Model.AllowSorting
        || Model.AllowViewModeChanging
        || Model.AllowFiltering;

    var listCssClasses = new List<string> { "artlist" };
    if (Model.ViewMode == ProductSummaryViewMode.List)
    {
        listCssClasses.Add("artlist-lines");
    }
    else
    {
        listCssClasses.Add("artlist-grid");

        if (Model.BoxedStyleItems)
        {
            listCssClasses.Add("artlist-boxed");
        }

        var colspan = (int)Model.GridColumnSpan;
        listCssClasses.Add("artlist-{0}-cols".FormatInvariant(colspan));
    }

    string artPictureWidth = null;
    if (Model.ViewMode == ProductSummaryViewMode.List)
    {
        var maxPictureSize = Model.ThumbSize ?? Model.Items.Select(x => x.Image)
            .Where(x => x != null)
            .Max(x => x.ThumbSize);

        if (maxPictureSize.HasValue)
        {
            artPictureWidth = $"--art-picture-width: {maxPictureSize.Value}px";
        }
    }
}

@if (Model.PagedList.TotalCount > 0 && hasActions)
{
    <div class="artlist-actions artlist-actions--top d-flex flex-wrap flex-lg-nowrap" attr-style='(imgRatio.HasValue(), "--img-aspect-ratio: " + imgRatio)'>
        <div sm-if="Model.AllowSorting || Model.AllowFiltering" class="artlist-action-group artlist-action-group--filtersort">
            <partial name="Product.List.FilterSort" model="Model" />
        </div>
        <div sm-if="Model.AllowViewModeChanging" class="artlist-action-group artlist-action-group--viewmode">
            <div sm-if="Model.AllowSorting" class="vr mx-2 text-body-tertiary" aria-hidden="true"></div>
            @* ViewMode *@
            @if (Model.AllowViewModeChanging && Model.ViewMode == ProductSummaryViewMode.Grid || Model.ViewMode == ProductSummaryViewMode.List)
            {
                <partial name="Product.List.ViewMode" model="Model" />
            }
        </div>
    </div>
}

@if (Model.AllowFiltering)
{
    <partial name="Search.ActiveFilters" model="Model.SourceResult" />
}

<div sm-if="Model.PagedList.TotalCount > 0"
     id="@id"
     class='@string.Join(" ", listCssClasses)'
     data-label-prev="@T("Aria.Label.ShowPreviousProducts")"
     data-label-next="@T("Aria.Label.ShowNextProducts")"
     attr-style='(artPictureWidth.HasValue(), artPictureWidth)'
     role="list">
    @foreach (var product in Model.Items)
    {
        <partial name="Product.List.Item" model="product" />
    }
</div>

<div sm-if="Model.PagedList.TotalPages > 1 && hasPager" class="artlist-pagination hstack mb-4">
    <partial name="Product.List.Pager" model="Model" />
</div>

